package com.neuedu.mblog.dao;

import com.neuedu.mblog.entiry.User;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * 项目：his
 * 创建时间：  2021/12/9   14:49
 * 作者 :jshand
 * 描述 : 用户的数据访问层
 */
public class UserDao extends BaseDao<User> {

    private QueryRunner run = new QueryRunner( dataSource );

    @Override
    public List<User> selectList(String sql) throws Exception {
        throw  new Exception("暂未实现的方法");
    }

    /**
     * 批量查询
     * @param sql
     * @param params
     * @return
     * @throws SQLException
     */
    @Override
    public List<User> selectList(String sql,Object ... params) throws SQLException {

        //每张表不一样的放，解析结果集
        List<User> userList = run.query(sql, new ResultSetHandler<List>() {
            @Override
            public List handle(ResultSet rs) throws SQLException {
                List list = new ArrayList();
                while(rs.next()) {
                    User user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                    user.setName(rs.getString("name"));
                    user.setAvatar(rs.getString("avatar"));
                    user.setEmail(rs.getString("email"));
                    user.setRoleId(rs.getInt("role_id"));
                    user.setPassword(rs.getString("password"));
                    user.setStatus(rs.getInt("status"));
                    user.setCreated(rs.getTimestamp("created"));
                    user.setLastLogin(rs.getTimestamp("last_login"));
                    user.setGender(rs.getInt("gender"));

                    list.add(user);
                    return list;
                }
                return list;
            }
        },params);

        return userList;
    }

    /**
     *
     * @param user 带保存的实体对象
     * @return  int 影响行数
     * @throws SQLException
     */
    @Override
    public int save(User user) throws SQLException {
        StringBuilder sql = new StringBuilder();
        sql.append(" INSERT INTO mto_user (  ");
        sql.append("   username, NAME,  avatar,  email,  PASSWORD, STATUS,  created,  updated,  last_login,  gender,   signature ");
        sql.append(" )  ");
        sql.append(" VALUES ");
        sql.append("   ( ");
        sql.append("    ?,?,?,?,?,?,?,?,?,?,? ");
        sql.append("   )  ");
        Object[] params = {
                user.getUsername(),
                user.getName(),
                user.getAvatar(),
                user.getEmail(),
                user.getPassword(),
                user.getStatus(),
                user.getCreated(),
                user.getUpdated(),
                user.getLastLogin(),
                user.getGender(),
                user.getSignature()
        };
        return run.update(sql.toString(),params);
    }

    /**
     * 根据主键更新
     * @param user
     * @return
     * @throws SQLException
     */
    @Override
    public int update(User user) throws SQLException {
        // role_id,  comments,  posts,  角色id，评论的数量  ，发表文章
        StringBuilder sql = new StringBuilder();
        sql.append(" UPDATE 				");
        sql.append("   mto_user             ");
        sql.append(" SET                    ");
        sql.append("   username =   ?,      ");
        sql.append("   NAME =  ?,           ");
        sql.append("   avatar =   ?,        ");
        sql.append("   email =   ?,         ");
        sql.append("   PASSWORD =   ?,      ");
        sql.append("   STATUS =   ?,        ");
        sql.append("   updated =   ?,       ");
        sql.append("   last_login =   ?,    ");
        sql.append("   gender =   ?,        ");
        sql.append("   signature =   ?      ");
        sql.append(" WHERE id= ?            ");
        Object[] params = {
                user.getUsername(),
                user.getName(),
                user.getAvatar(),
                user.getEmail(),
                user.getPassword(),
                user.getStatus(),
                user.getUpdated(),
                user.getLastLogin(),
                user.getGender(),
                user.getSignature(),
                user.getId()
        };
        return run.update(sql.toString(),params);

    }

    @Override
    public int delete(Integer id) throws SQLException {
        String sql = "delete from mto_user where id = ?";
        Object[] params = {id};
        return run.update(sql,params);
    }

    @Override
    public User selectById(Integer id) throws SQLException {


        //参数列表
        Object[] params = {id};

        String sql = "select * from mto_user where id = ?";
        User user = run.query(sql,new ResultSetHandler<User>() {
            @Override
            public User handle(ResultSet rs) throws SQLException {
                User user = null;
                if(rs.next()) {
                    user = new User();
                    user.setId(rs.getInt("id"));
                    user.setUsername(rs.getString("username"));
                    user.setName(rs.getString("name"));
                    user.setAvatar(rs.getString("avatar"));
                    user.setEmail(rs.getString("email"));
                    user.setPassword(rs.getString("password"));
                    user.setStatus(rs.getInt("status"));
                    user.setCreated(rs.getTimestamp("created"));
                    user.setLastLogin(rs.getTimestamp("last_login"));
                    user.setGender(rs.getInt("gender"));
                }
                return user;
            }
        },params);

        return user;
    }


}
